package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"service/basic/config"
	"service/basic/global"
	"service/internal/model"
)

var err error

func InitMysql() {
	var mysqlConfig = config.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True",
		mysqlConfig.User,
		mysqlConfig.Password,
		mysqlConfig.Host,
		mysqlConfig.Port,
		mysqlConfig.Database)

	global.DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		panic(err)
	}
	fmt.Println("mysql数据库连接成功")
	err = global.DB.AutoMigrate(&model.Pet{}, &model.Address{}, &model.Beauty{}, &model.ComboDetail{}, &model.Order{})
	if err != nil {
		panic(err)
	}

	fmt.Println("数据表迁移成功")
}
